﻿namespace FFF.Test.Util.Setting
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Abp;
    using Abp.Configuration;
    using Abp.Localization;
    using Abp.Net.Mail;
    using FFF.Test.Util.Consts;

    /// <summary>
    /// appSetting静态配置
    /// </summary>
    public static class AppSettings
    {
        /// <summary>
        /// 邮件的配置
        /// </summary>
        public static string MyMailSetting { get; set; }
    }

    /// <summary>
    /// setting的配置类 apperseice和domainservice可以通过调用
    /// <code>
    /// <![CDATA[
    /// var value1 = await SettingManager.GetSettingValueAsync<bool>("PassiveUsersCanNotLogin");    /// var value2 = SettingManager.GetSettingValue("SmtpServerAddress");
    /// ]]>
    /// </code>
    public class MySettingProvider : SettingProvider
    {
        /// <summary>
        /// 初始化setting的配置
        /// </summary>
        /// <param name="context">setting上下文</param>
        /// <returns>setting的配置</returns>
        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            List<SettingDefinition> settings = new List<SettingDefinition>();

            var testSetting = new List<SettingDefinition>()
            {
                new SettingDefinition(
                    "SmtpServerAddress",
                    "127.0.0.1"
                    ),

                new SettingDefinition(
                    "PassiveUsersCanNotLogin",
                    "true",
                    scopes: SettingScopes.Application | SettingScopes.Tenant
                    ),

                new SettingDefinition(
                    "SiteColorPreference",
                    "red",
                    scopes: SettingScopes.User,
                    clientVisibilityProvider: new VisibleSettingClientVisibilityProvider()
                    )

            };
            settings.AddRange(testSetting);
            var mailSetting = new List<SettingDefinition>()
            {
                new SettingDefinition(
                    EmailSettingNames.Smtp.Host,
                    AppConfigConsts.EmailSetting.SmtpHost,
                    L("SmtpHost"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.Port,
                    AppConfigConsts.EmailSetting.EmailPort,
                    L("SmtpPort"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.UserName,
                    AppConfigConsts.EmailSetting.UserName,
                    L("Username"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.Password,
                    AppConfigConsts.EmailSetting.UserPwd,
                    L("Password"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.Domain,
                    string.Empty,
                    L("Domain"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.EnableSsl,
                    "false",
                    L("EnableSsl"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                 new SettingDefinition(
                    EmailSettingNames.Smtp.UseDefaultCredentials,
                    "false",
                    L("UseDefaultCredentials"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                  new SettingDefinition(
                    EmailSettingNames.DefaultFromAddress,
                    AppConfigConsts.EmailSetting.UserName,
                    L("DefaultFromAddress"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

                  new SettingDefinition(
                    EmailSettingNames.DefaultFromDisplayName,
                    AppConfigConsts.EmailSetting.DisplayName,
                    L("DefaultFromDisplayName"),
                    scopes: SettingScopes.Application | SettingScopes.Tenant),

            };
            settings.AddRange(mailSetting);

            return settings;
        }

        private static LocalizableString L(string name)
        {
            return new LocalizableString(name, AbpConsts.LocalizationSourceName);
        }
    }
}
